﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
using System.Globalization;
using System.IO;

namespace Pixysoft.Framework.Logs.Core
{
    class LogIdCommand
    {
        bool crossFiles = false;
        string filepath;
        string fileFullName;
        string id = null;

        public LogIdCommand(string filepath, string fileFullName, string id)
        {
            this.id = id;
            this.filepath = filepath;
            this.fileFullName = fileFullName;
        }

        public LogIdCommand(string filepath, string fileFullName, string id, bool crossFiles)
        {
            this.id = id;
            this.filepath = filepath;
            this.fileFullName = fileFullName;
            this.crossFiles = crossFiles;
        }


        public ILogRecord ExecuteReader()
        {
            foreach (string file in GetSearchFiles())
            {
                IXmlTree tree = GetXmlTree(file);

                if (tree.Node.Count == 0)
                    continue;

                IXmlNode node = tree.Node[0];

                ILogRecord record = new LogRecord(node);

                //record.Id = node.Properties[StringHelper.default_attribute_id];
                //record.Ip = node.Properties[StringHelper.default_attribute_ip];
                //record.Level = StringHelper.GetLogLevel(node.Properties[StringHelper.default_attribute_level]);
                //record.RawDate = node.Properties[StringHelper.default_attribute_time];
                record.Filepath = file;

                //if (node.Node.Contains(StringHelper.default_element_message))
                //    record.Text = node.Node[StringHelper.default_element_message].Text;
                //else
                //    record.Text = node.Text;

                //if (node.Node.Contains(StringHelper.default_element_frame))
                //{
                //    foreach (IXmlNode subnode in node.Nodes[StringHelper.default_element_frame])
                //    {
                //        record.Frames.Add(
                //            subnode.Properties[StringHelper.default_attribute_dll],
                //            subnode.Properties[StringHelper.default_attribute_class],
                //            subnode.Properties[StringHelper.default_attribute_method],
                //            subnode.Properties[StringHelper.default_attribute_path],
                //            subnode.Properties[StringHelper.default_attribute_file],
                //            subnode.Properties[StringHelper.default_attribute_line],
                //            subnode.Properties[StringHelper.default_attribute_column]);
                //    }
                //}

                return record;
            }

            return null;
        }


        private List<string> GetSearchFiles()
        {
            List<string> list = new List<string>();

            if (File.Exists(filepath + fileFullName))
                list.Add(filepath + fileFullName);

            if (!crossFiles)
            {
                return list;
            }

            foreach (string file in GetCrossFiles())
            {
                list.Add(file);
            }

            return list;
        }

        /// <summary>
        /// 取得区间内的搜索文本
        /// </summary>
        /// <returns></returns>
        private List<string> GetCrossFiles()
        {
            List<string> list = new List<string>();

            //10-02-06 如果不存在 返回空 不抛异常

            if (!Directory.Exists(filepath))
                return list;

            //if (!Directory.Exists(filepath))
            //    throw new Exception("file path do not exist." + filepath);

            string searchfilename = fileFullName.Substring(0, fileFullName.LastIndexOf('.'));

            foreach (string file in Directory.GetFiles(filepath))
            {
                string dfilename = StringFilter.GetFileName(file);

                if (string.IsNullOrEmpty(dfilename))
                    continue;

                if (dfilename.Equals(fileFullName, StringComparison.OrdinalIgnoreCase))
                    continue;

                if (!dfilename.EndsWith(StringHelper.default_log_postfix, StringComparison.OrdinalIgnoreCase))
                    continue;

                if (!dfilename.StartsWith(searchfilename, StringComparison.OrdinalIgnoreCase))
                    continue;

                list.Add(file);
            }

            list.Sort();

            return list;
        }

        /// <summary>
        /// 取得搜索结果
        /// </summary>
        /// <returns></returns>
        private IXmlTree GetXmlTree(string file)
        {
            IXmlCommand command = XmlManager.CreateCommand(file);

            command.Element = StringHelper.default_element_log;

            command.Constraints.And(StringHelper.default_attribute_id).Equal(id);

            IXmlTree tree = command.ExecuteReader();

            return tree;
        }

        public bool CrossFiles
        {
            get
            {
                return crossFiles;
            }
            set
            {
                crossFiles = value;
            }
        }
    }
}
